const commonutil = require("../commonutil.js");
const screenresultservice = require("../../../apiservice/screen/screenresultservice.js");
const jsonData = require("../jsondata.js");
const app = getApp();
const regeneratorRuntime = app.globalData.regeneratorRuntime;
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		scid: '',
		customerid: '',
		leadName: '',
		categoryid: '',
		addOrMod: "add",
		//白内障选项集
		lensStatusIndex: -1,
		lensOpacityStatusIndex: -1,
		cataractIndex: -1,
		cataractArray: jsonData.noneOrBothOrLeftOrRight,
		lensStatusArray: jsonData.normalOrAbnormal,
		lensOpacityStatusArray: jsonData.lensOpacityStatus,
		adviseshow: false,
    schoolId:'',
		actions: [{
				name: '左眼白内障术后'
			},
			{
				name: '右眼白内障术后'
			},
			{
				name: '双眼白内障术后'
			},

		],
		/**白内障检查 */
		//晶状体
		lensStatus: {
			new_scid: null,
			new_categoryid: 'LB0001',
			new_itemid: 'XM0001',
			new_contentid: 'NR0001',
			new_result: 'SC0000000013',
			new_doctorid: null,
			ownerid: null
		},
		//晶状体浑浊程度
		lensOpacityStatus: {
			new_scid: null,
			new_categoryid: 'LB0001',
			new_itemid: 'XM0001',
			new_contentid: 'NR0002',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//具体描述
		specificDescription: {
			new_scid: null,
			new_categoryid: 'LB0001',
			new_itemid: 'XM0001',
			new_contentid: 'NR0003',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//初步诊断
		cataract: {
			new_scid: null,
			new_categoryid: 'LB0001',
			new_itemid: 'XM0002',
			new_contentid: 'NR0004',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//类型
		cataractType: {
			new_scid: null,
			new_categoryid: 'LB0001',
			new_itemid: 'XM0002',
			new_contentid: 'NR0006',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},

	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var options = options;
		if (options) {
			this.setData({
				leadName: options.leadname,
				customerid: options.sccustid,
				scid: options.scid,
				categoryid: options.categoryid
			})
			this.getCataraResults(options.scid, options.sccustid, options.categoryid);
      if (options.schoolId) {
        this.setData({
          schoolId: options.schoolId
        })
      }
		}

	},
	//白内障检查提交
	cataractSubmit: function() {
		var createResult = this.getCataract();
		//createResult.ownerid = 
		var that = this;
		commonutil.createScreenResults(createResult, function(res) {
			var idArray = res.result.split('|');
			for (var i = 0; i < idArray.length; i++) {
				createResult[i].new_sc_resultId = idArray[i];
			}
			that.setData({
				addOrMod: "mod",
				lensStatus: createResult[0],
				lensOpacityStatus: createResult[1],
				specificDescription: createResult[2],
				cataract: createResult[3],
				cataractType: createResult[4],
			});
		});
	},
	//白内障检查更新
	cataractUpdate: function() {
		var updateResult = this.getCataract();
		commonutil.updateScreenResults(updateResult);
	},
	//查看报告
	enterDetail: async function(e) {
		var scid = this.data.scid;
		var leadid = this.data.customerid;
		var lead = {};
		var data = await commonutil.getLeadInfo(leadid);
		lead.leadid = data.leadId;
		lead.customername = data.lastName;
		lead.age = data.new_age;
		lead.sex = data.new_gender;
		lead.iphoneno1 = data.new_phone;
		lead.sfz = data.new_citizenid;
		lead.createdon = data.modifiedOn;
    var url = "/pages/print/letter/letter?scid=" + scid;
    if(this.data.schoolId)
      url +="&schoolId="+this.data.schoolId;
		wx.navigateTo({
			url: url,
			events: {
				// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
				acceptDataFromOpenedPage: function(data) {
					console.log(data)
				},
				someEvent: function(data) {
					console.log(data)
				}
			},
			success: function(res) {
				// 通过eventChannel向被打开页面传送数据
				res.eventChannel.emit('acceptDataFromOpenerPage', {
					data: lead
				})
			}
		})
	},
	//获取结果集
	getCataraResults: async function(scid, customerid, categoryid) {
		wx.showLoading({
			title: '加载中...',
			mask: true,
		})
		var success = await screenresultservice.GetScreenResultByCategory(scid, customerid, categoryid);
		var reusltArray = success.data
		if (reusltArray.length == 0) {
			wx.hideLoading();
			return;
		}
		this.setData({
			addOrMod: 'mod'
		})
		for (let index in reusltArray) {
			var element = reusltArray[index];
			if (element.new_contentid === 'NR0001') {
				this.setData({
					lensStatus: element
				})
			} else if (element.new_contentid === 'NR0002') {
				this.setData({
					lensOpacityStatus: element
				})
			} else if (element.new_contentid === 'NR0003') {
				this.setData({
					specificDescription: element
				})
			} else if (element.new_contentid === 'NR0004') {
				this.setData({
					cataract: element
				})
			} else if (element.new_contentid === 'NR0006') {
				this.setData({
					cataractType: element
				})
			}
		}
		wx.hideLoading();
	},
	/**白内障检查选项集、文本赋值 */
	onLensStatusChange: function(e) {
		var result = 'lensStatus.new_result';
		this.setData({
			[result]: '',
			lensStatusIndex: e.detail.index
		})
	},
	onlensOpacityStatusChange: function(e) {
		this.setData({
			lensOpacityStatusIndex: e.detail.index
		})
	},
	onCataractChange: function(e) {
		this.setData({
			cataractIndex: e.detail.index
		})
	},
	onSpecificDescriptionChange: function(e) {
		var specificDescriptionResult = 'specificDescription.new_result';
		this.setData({
			[specificDescriptionResult]: e.detail
		})
	},
	onCataractTypeChange: function(e) {
		var cataractTypeResult = 'cataractType.new_result';
		this.setData({
			[cataractTypeResult]: e.detail
		})
	},
	//打开建议
	onClickIcon: function(e) {
		this.setData({
			adviseshow: true
		})
	},
	//关闭建议
	adviseonClose: function() {
		this.setData({
			adviseshow: false
		})
	},
	//选择建议
	adviseonSelect: function(e) {
		//console.log(e);
		this.setData({
			adviseshow: false,
			['cataractType.new_result']: e.detail.name
		})
	},
	//白内障检查
	getCataract: function() {
		var createResult = [];
		var lensStatus = this.data.lensStatus;
		var lensOpacityStatus = this.data.lensOpacityStatus;
		var specificDescription = this.data.specificDescription;
		var cataract = this.data.cataract;
		var cataractType = this.data.cataractType;
		var scid = this.data.scid;
		var customerid = this.data.customerid;
		//晶状体
		var lensStatusIndex = this.data.lensStatusIndex;
		var lensStatusArray = this.data.lensStatusArray;
		if (lensStatusIndex > -1) {
			lensStatus.new_result = lensStatusArray[lensStatusIndex];
		}
		//晶状体浑浊程度
		var lensOpacityStatusIndex = this.data.lensOpacityStatusIndex;
		var lensOpacityStatusArray = this.data.lensOpacityStatusArray;
		if (lensOpacityStatusIndex > -1) {
			lensOpacityStatus.new_result = lensOpacityStatusArray[lensOpacityStatusIndex]
		}
		//白内障
		var cataractIndex = this.data.cataractIndex;
		var cataractArray = this.data.cataractArray;
		if (cataractIndex > -1) {
			cataract.new_result = cataractArray[cataractIndex];
		}
		
		createResult.push(lensStatus);
		if(lensStatus.new_result=='SC0000000005'&&lensOpacityStatus.new_result!=''){
			createResult.push(lensOpacityStatus);
		}		
		if(lensStatus.new_result=='SC0000000005'&&specificDescription.new_result!=''){
			createResult.push(specificDescription);
		}	
		createResult.push(cataract);
		if(cataractType.new_result!=''){
			createResult.push(cataractType);
		}
		//给各结果的筛查id/联系人id赋值
		createResult.forEach(element => {
			element.new_scid = scid;
			element.new_customerid = customerid;
		});
		return createResult;
	},

})
